package com.monitorRoutine.monitorRoutineServer.clt;

import com.monitorRoutine.monitorRoutineServer.entity.SqlRecord;
import com.monitorRoutine.monitorRoutineServer.entity.SqlResult;
import com.monitorRoutine.monitorRoutineServer.entity.SqlResultVo;
import com.monitorRoutine.monitorRoutineServer.service.MonitorSqlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/monitorSql")
@Api(tags = "sql执行 API")
public class MonitorSqlController {
    @Autowired
    MonitorSqlService monitorSqlService;
    @PostMapping("/executeSql")
    @ApiOperation(value = "sql执行-执行sql")
    public ArrayList<SqlResultVo> executeSql(@RequestBody List<SqlRecord> sqlRecords) throws IOException {
        List<SqlResult> sqlResults = monitorSqlService.executeSql(sqlRecords);
        ArrayList<SqlResultVo> sqlResultVos = new ArrayList<>();
        for (int i = 0; i < sqlRecords.size(); i++) {
            SqlResultVo sqlResultVo = new SqlResultVo(sqlResults.get(i), sqlRecords.get(i));
            sqlResultVos.add(sqlResultVo);
        }

        return sqlResultVos;
    }
    @PostMapping("/addSqlRecord")
    @ApiOperation(value = "sql执行-添加sql")
    public boolean addSqlRecord(@RequestBody SqlRecord sqlRecord){
       return monitorSqlService.addSqlRecord(sqlRecord);
    }

}
